package com.yj.provider.handlerMethodArgument;

import java.util.List;

public class PageableResponse<T> {

    private int page;
    private int pageSize;
    private int total;
    private List<T> data;

    public PageableResponse(int page,int pageSize,List data){
        this.page = page;
        this.pageSize = pageSize;
        this.data = data;
    }

//    public  PageableResponse(List data, int page, int pageSize) {
//        this(data.size(), data.subList((page - 1) * pageSize, pageSize), page, pageSize);
//    }
//
//    public PageableResponse(Integer total, List<T> data, int page, int pageSize) {
//        super();
//        this.total = total;
//        this.data = data;
//        this.page = page;
//        this.pageSize = pageSize;
//    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotal() {
        return data.size() % pageSize>0 ? pageSize+1 : pageSize;
    }

//    public void setTotal(int total) {
//        this.total = total;
//    }

    public List<T> getData() {
       if (this.data == null){
           return null;
       }
       if (this.data.size() == 0){
           return null;
       }

       int fromIndex = 0;
       int toIndex = 0;

       total = data.size() % pageSize == 0 ? data.size()/pageSize : data.size()/pageSize+1;

       if (page != total){
           fromIndex = (page-1)*pageSize;
           toIndex = fromIndex+pageSize;
       }else {
           fromIndex = (page-1)*pageSize;
           toIndex = data.size();
       }
       return data.subList(fromIndex,toIndex);
    }

    public void setData(List<T> data) {
        this.data = data;
    }


}
